from datetime import date, timedelta

from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.views import APIView

from goods.models import GoodsVisitCount
from users.models import User


class UserCountView(APIView):
    '''
    用户总量统计

    '''
    # 指定管理员权限
    permission_classes = [IsAdminUser]
    def get(self, request):
        # 获取注册用户总数（普通用户）
        count = User.objects.filter(is_staff=False).count()
        # 返回数量
        return Response({'count': count})


class UserDayCountView(APIView):
    '''
    当天用户注册总数
    '''
    permission_classes = [IsAdminUser]
    def get(self, request):
        # 获取当前日期
        now_date = date.today()
        # 获取当日注册用户数量 date_joined 记录创建账户时间
        count = User.objects.filter(is_staff=False, date_joined__gte=now_date).count()
        return Response({
            "count": count
                    })

class UserActiveCountView(APIView):
    '''
    日活用户统计
    '''
    permission_classes = [IsAdminUser]
    def get(self, request):
        # 获取当前日期
        now_date = date.today()
        # 获取当日用户登录数量 date_joined
        count = User.objects.filter(is_staff=False, last_login__gte=now_date).count()
        return Response({
            "count": count
        })


class UserOrderCountView(APIView):
    # 指定管理员权限
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 获取当前日期
        now_date = date.today()
        # 获取当日下单用户数量  orders__create_time 订单创建时间
        users = User.objects.filter(is_staff=False, orders__create_time__gte=now_date)
        # 去重
        user = set(users)
        count = len(user)
        #　返回数量
        return Response({
            "count": count
        })



class UserMonthCountView(APIView):
    # 指定管理员权限
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 获取当前日期
        now_date = date.today()
        # 获取一个月前日期
        old_date = now_date - timedelta(30)
        # 创建空列表保存每天的用户量
        date_list = []

        for i in range(35):
            # 循环遍历获取当天日期
            index_date = old_date + timedelta(i)
            # 指定下一天日期
            next_date = old_date + timedelta(i + 1)
            # 查询条件是大于当前日期index_date，小于明天日期的用户cur_date，得到当天用户量
            count = User.objects.filter(is_staff=False, date_joined__gte=index_date, date_joined__lt=next_date).count()

            date_list.append({
                'count': count,
                'date': index_date
            })
        # 返回数量
        return Response(date_list)


class GoodsCountView(APIView):
    '''
    日分类商品访问量
    '''
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 获取当天日期
        now_date = date.today()
        # 获取当天访问的商品分类数量信息
        goods = GoodsVisitCount.objects.filter(date__gte=now_date)
        data_list = []
        for good in goods:
            count = good.count
            # 获取关联分类对象的名字
            category = good.category.name
            data_list.append({'count':count, 'category':category})
        #　返回数量
        return Response(data_list)